<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/12
 * Time: 下午3:11
 */

namespace Model\Admin;


use Core\DB_Mysqli;
use Data\Company\Builder\CompanyContentBuilder;
use Data\Company\CompanyCatlogModel;
use Data\Company\CompanyContentModel;
use Data\Company\CompanyModel;

class CompanyController extends BaseController
{
    /**
     *
     */
    public function index(){
        global $_G,$_lang;

        $model = new CompanyModel();
        if ($this->checkFormSubmit()){
            $delete = $_GET['delete'];
            if ($delete) {
                $contentModel = new CompanyContentModel();
                foreach ($delete as $company_id){
                    $model->where(array('company_id'=>$company_id))->delete();
                    $contentModel->where(array('company_id'=>$company_id))->delete();
                }
            }
            $this->showAjaxReturn();
            //$this->showSuccess('delete_succeed');
        }else {

            $pagesize = 20;
            $condition = array();

            $totalcount  = $model->where($condition)->count();
            $pagecount   = $totalcount < $pagesize ? 1 : ceil($totalcount/$pagesize);
            $companylist = $model->where($condition)->page($_G['page'], $pagesize)->order('company_id DESC')->select();
            $pagination = $this->pagination($_G['page'], $pagecount, $totalcount, "", true);

            /*$db = new DB_Mysqli();
            $sql = "SELECT c1.*, c2.name AS cat_name FROM ".$db->table('company')." c1 LEFT JOIN ".$db->table('company_catlog').
            " c2 ON c2.catid=c1.catid ORDER BY company_id DESC";
            $query = $db->query($sql);

            $companylist = array();
            while ($data = $db->fetch_array($query)){
                $companylist[] = $data;
            }*/
            /*$companylist = M('company c1')->field('c1.*,c2.name AS cat_name')
                ->join('company_catlog c2', 'c2.catid=c1.catid')->order('company_id', 'DESC')->select();*/

            $catloglist = (new CompanyCatlogModel())->getCache();
            //print_array($catloglist);exit();

            include template('company/company_list');
        }
    }

    /**
     * 添加企业信息
     */
    public function add(){
        global $_G,$_lang;

        $this->showError('功能已关闭， 请从前台个人中心录入');
        if ($this->checkFormSubmit()){
            $company = $_POST['company'];
            if ($company['company_name'] && $company['company_logo']) {
                $builder = new CompanyContentBuilder();
                $builder->setData($company);
                $builder->setUid($this->uid);
                $company_id = (new CompanyModel())->addObject($builder);

                $contentModel = new CompanyContentModel();
                $contentModel->data(array(
                    'uid'=>$this->uid,
                    'company_id'=>$company_id,
                    'content'=>trim($_GET['content'])
                ))->add();
                $this->showSuccess('save_succeed');
            }else {
                $this->showError('invalid_parameter');
            }
        }else {
            //导入模板
            $editorname = 'content';
            $editorcontent = '';

            $catlogModel = new CompanyCatlogModel();
            $catloglist = $catlogModel->getCatlogTree();
            include template('company/company_form');
        }
    }

    /**
     * 编辑企业信息
     */
    public function edit(){
        global $_G,$_lang;

        $company_id = intval($_GET['company_id']);
        $condition = array('company_id'=>$company_id);

        if ($this->checkFormSubmit()){
            $company = $_POST['company'];
            $content = $_GET['content'];

            if ($company['company_name'] && $company['company_logo']) {
                $company['update_time'] = time();
                //修改企业信息
                $model = new CompanyModel();
                $model->data($company)->where($condition)->save();
                //修改企业简介
                $contentModel = new CompanyContentModel();
                $contentModel->data(array('content'=>trim($_POST['content'])))->where($condition)->save();
                $this->showSuccess('update_succeed');
            }else {
                $this->showError('invalid_parameter');
            }
        }else {
            $model = new CompanyModel();
            $company = $model->where($condition)->getOne();
            $content = (new CompanyContentModel())->where($condition)->getOne();

            $editorname = 'content';
            $editorcontent = $content['content'];

            $catlogModel = new CompanyCatlogModel();
            $catloglist = $catlogModel->getCatlogTree();
            include  template('company/company_form');
        }
    }
}

